package com.ebelter.nb.ui.activitys;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CountDownTextView;
import com.ebelter.nb.R;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;

public class FindPswActivity1 extends NBBaseActivity implements View.OnClickListener {

    public static final String TAG = "FindPswActivity1";
    ImageView top_left_iv;
    TextView m_title_tv;
    TextView m_righttitle_tv;

    EditText cb1_ed;
    EditText cb2_ed;

    CountDownTextView register_countDownTv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_phoneregister;
    }


    @Override
    protected void initView() {
        super.initView();
        FV();
        listeners();
    }

    private void listeners() {
        register_countDownTv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);
    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);
        m_title_tv=findViewById(R.id.m_title_tv);
        m_righttitle_tv=findViewById(R.id.m_righttitle_tv);

        cb1_ed = findViewById(R.id.cb1_ed);
        cb2_ed = findViewById(R.id.cb2_ed);
        Button bt = findViewById(R.id.register_bt);
        bt.setText("下一步");

        String phone = UserSpUtil.readString(NbConstants.User.phone);
        if (!TextUtils.isEmpty(phone)){
            ViewUtils.setTextViewStr(cb1_ed,phone);
        }

        ViewUtils.displayView(m_title_tv);
        ViewUtils.setTextViewStr(m_title_tv,"找回密码");
        ViewUtils.goneView(m_righttitle_tv);


        register_countDownTv=findViewById(R.id.register_countDownTv);
    }

    //登录按钮
    public void register(View view) {

        String phone = cb1_ed.getText().toString().trim();
        String phoneCode = cb2_ed.getText().toString().trim();

        if (TextUtils.isEmpty(phone)){
            ToastUtil.show("手机号码不能为空");
            return;
        }

        if (!VerifyUtils.isMobile(phone)){
            ToastUtil.show("手机号码格式不正确");
            return;
        }

        if (TextUtils.isEmpty(phoneCode)){
            ToastUtil.show("验证码不能为空");
            return;
        }

        Intent intent = new Intent(this,FindPswActivity2.class);
        intent.putExtra("phone",phone);
        intent.putExtra("phoneCode",phoneCode);
        startActivity(intent);


    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid==R.id.register_countDownTv){
            requestPhoneCode();
        }else if (vid==R.id.top_left_iv){
            finish();
        }
    }

    //获取验证码
    private void requestPhoneCode() {

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        String phone = cb1_ed.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            ToastUtil.show("手机号码不能为空");
            return;
        }

        if (!VerifyUtils.isMobile(phone)){
            ToastUtil.show("手机号码格式不正确");
            return;
        }

        NbNetUtils.getInstance().requestPhoneCode(this, phone, new HttpResponse2<NbBaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode, NbBaseResponse nbBaseResponse, String s) {
                LogUtils.i("PhoneRegisterActivity","--requestPhoneCode = "+s);
                if (isRequestSuccess){
                    if (requestResultCode==0){
                        //成功
                        register_countDownTv.setCountSecond(60);//{"resultCode": 0,"resultMessage": "成功"}
                        register_countDownTv.click();
                    }else {
                        if (nbBaseResponse!=null){
                            ToastUtil.show(nbBaseResponse.message);
                        }else {
                            //失败
                            String ms = "请求手机验证码失败";
                            if (requestResultCode==1){
                                ms = "发送频率限制,请120秒后获取！";
                            }
                            ToastUtil.show(ms);
                        }

                    }

                }else {
                    //失败
                    String ms = "请求手机验证码失败";
                    if (requestResultCode==1){
                        ms = "发送频率限制,请120秒后获取！";
                    }
                    ToastUtil.show(ms);
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (register_countDownTv!=null){
            register_countDownTv.exitActivity();
            register_countDownTv=null;
        }
    }
}
